package com.hzhr.iot.login.service;

import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.login.Global;
import com.hzhr.iot.login.dao.UserCardDao;
import com.hzhr.iot.login.entity.UserCardEntity;
import com.hzhr.iot.login.model.userCard.UserCardGetModel;
import com.hzhr.iot.login.model.userCard.UserCardModel;
import com.hzhr.iot.login.service.base.BaseService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

@Service
public class UserCardService extends BaseService {
    @Autowired
    UserCardDao tokenDao;
    @Autowired
    Global global;

    //region javadoc注释 : getUserCard
    /**
     * 查询用户名片 <br/><br/>
     * 简介&nbsp;:&nbsp;所有用户名片 <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;查询用户名片&nbsp;:&nbsp;根据路径参数“令牌”查询用户卡片“用户卡片_1”；(√) <br/>
     * 2.&nbsp;返回信息&nbsp;:&nbsp;根据“用户卡片_1”生成并返回“用户名片模型(查询)”；(√) <br/>
     * @param header <br/><br/>
     * @param query <br/><br/>
     * @param token 令牌 <br/><br/>
     * @throws ServiceException <br/>
     * @see UserCardGetModel result <br/>
     */ 
    //endregion
    //region 说明 : getUserCard
    //查询用户名片
    //简介 : 所有用户名片
    //步骤 :
    //1. 查询用户名片 : 根据路径参数“令牌”查询用户卡片“用户卡片_1”；(√)
    //2. 返回信息 : 根据“用户卡片_1”生成并返回“用户名片模型(查询)”；(√)
    //endregion
    //region 参数 : getUserCard
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "path"),
    })
    //endregion
    //region 请求 : getUserCard
    @RequestMapping(value = "/user-cards/{token}", method = RequestMethod.GET)
    //endregion
    public UserCardGetModel getUserCard(Map<String, String> header, String token, Map<String, Object> query) throws ServiceException {
        /* 查询用户名片 */
        UserCardModel userCard = null;
        String globalToken = global.getToken();
        boolean tokenFlag = false;
        String[] tokenSplit = globalToken.split("_");
        for (String item:
                tokenSplit) {
            if (item.equals(token)) {
                tokenFlag = true;
            }
        }
        if (tokenFlag) {
            userCard = new UserCardModel();
            userCard.setUserId(0);
            userCard.setUserName("系统");
            userCard.setUserCode("0");
            userCard.setCompanyId(0);
            userCard.setDeptId(0);
            userCard.setType("0");
        } else {
            UserCardEntity userCardEntity = null;
            userCardEntity = tokenDao.selectByToken(token);
            if (userCardEntity != null) {
                userCard = new UserCardModel();
                userCard.setUserId(userCardEntity.getUserId());
                userCard.setUserName(userCardEntity.getUserName());
                userCard.setUserCode(userCardEntity.getUserCode());
                userCard.setCompanyId(userCardEntity.getCompanyId());
                userCard.setDeptId(userCardEntity.getDeptId());
                userCard.setType(userCardEntity.getType());
            }
        }

        /* 返回信息 */
        UserCardGetModel result = new UserCardGetModel();
        result.setModel(userCard);
        return result;
    }
}





















































































































































































































